
<!DOCTYPE html>
<html lang="">


<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1.0, user-scalable=no">
  <meta name="theme-color" content="#202020"/>
  <meta http-equiv="x-ua-compatible" content="ie=edge">
  <script src="/js/busuanzi.pure.mini.js" async></script>
  
  
    <meta name="keywords" content="Emberjs,Ember-Teach,Ember-Template," />
  

  
    <meta name="description" content="一个专注一coding的网站。提供丰富编程知识，包括Spring、Java、EmberJS、SpringBoot等等技术。" />
  
  
  <link rel="icon" type="image/x-icon" href="/image/favicon.ico">
  <title>action helper [ Keep Coding ]</title>
  
    <!-- stylesheets list from config.yml -->
    
      <link rel="stylesheet" href="/css/pure-min.css">
    
      <link rel="stylesheet" href="/css/xoxo.css">
    
  
<meta name="generator" content="Hexo 5.0.2"><link rel="alternate" href="/atom.xml" title="Keep Coding" type="application/atom+xml">
</head>

<body>
  <div class="nav-container">
    <nav class="home-menu pure-menu pure-menu-horizontal">
  <a class="pure-menu-heading" href="/">
    <img class="avatar" src="/image/favicon.ico">
    <span class="title">Keep Coding</span>
  </a>

  <ul class="pure-menu-list clearfix">
      
          
            <li class="pure-menu-item"><a href="/" class="pure-menu-link">首页</a></li>
          
      
          
            <li class="pure-menu-item"><a href="/archives" class="pure-menu-link">归档</a></li>
          
      
          
            <li class="pure-menu-item"><a href="/tags" class="pure-menu-link">标签</a></li>
          
      
          
            <li class="pure-menu-item"><a href="/search" class="pure-menu-link">搜索</a></li>
          
      
          
            <li class="pure-menu-item"><a href="/about" class="pure-menu-link">关于</a></li>
          
      
          
            <li class="pure-menu-item"><a href="/atom.xml" class="pure-menu-link">订阅</a></li>
          
      
  </ul>
   
</nav>
  </div>

  <div class="container" id="content-outer">
    <div class="inner" id="content-inner">
      <div class="post-container">
  <article class="post" id="post">
    <header class="post-header text-center">
      <h1 class="title">
        action helper
      </h1>
      <span>
        
        <time class="time" datetime="2020-01-18T14:50:37.000Z">
        2020-01-18
      </time>
        
      </span>
      <span class="slash">/</span>
      <span class="post-meta">
      <span class="post-tags">
        <ul class="article-tag-list" itemprop="keywords"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/Ember-Teach/" rel="tag">Ember-Teach</a></li><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/Ember-Template/" rel="tag">Ember-Template</a></li><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/Emberjs/" rel="tag">Emberjs</a></li></ul>
      </span>
    </span>
      <span class="slash">/</span>
      <span class="read">
      <span id="busuanzi_value_page_pv"></span> 点击
    </span>
      <span class="slash">/</span>
      <span class="read">阅读耗时 4 分钟</span>
    </header>

    <div class="post-content">
      <p><code>action</code>助手所现实的功能与<code>javascript</code>里的事件是相似的，都是通过用户点击元素触发定义在元素上的事件。Ember的action助手还允许你传递参数到对应的<code>controller</code>、<code>component</code>类，在<code>controller</code>或者<code>component</code>上处理事件的逻辑。<br>准备工作，我们使用<a target="_blank" rel="noopener" href="http://ember-cli.com/user-guide">Ember CLI</a>命令创建一个名称为<code>myaction</code>的<code>controller</code>和同名的<code>route</code>，如果你不知道怎么使用Ember CLI请看前面的文章<a target="_blank" rel="noopener" href="http://blog.ddlisting.com/2016/03/18/ember-js-ru-men-zhi-nan-zhi-qi-di-zhang-dui-xiang-mo-xing-xiao-jie/">Ember.js 入门指南之七第一章对象模型小结</a>，这篇文件讲解了怎么使用Ember CLI构建一个简单的Ember项目。</p>
<h3 id="1，action使用实例"><a href="#1，action使用实例" class="headerlink" title="1，action使用实例"></a>1，action使用实例</h3><h4 id="1，在route层增加测试数据"><a href="#1，在route层增加测试数据" class="headerlink" title="1，在route层增加测试数据"></a>1，在route层增加测试数据</h4><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//  apap/routes/myaction.js</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> Ember <span class="keyword">from</span> <span class="string">&#x27;ember&#x27;</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> Ember.Route.extend(&#123;</span><br><span class="line">    <span class="comment">//  返回测试数据到页面</span></span><br><span class="line">    model: <span class="function"><span class="keyword">function</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> &#123; <span class="attr">id</span>:<span class="number">1</span>, <span class="attr">title</span>: <span class="string">&#x27;ACTIONS&#x27;</span>, <span class="attr">body</span>: <span class="string">&quot;Your app will often need a way to let users interact with controls that change application state. For example, imagine that you have a template that shows a blog title, and supports expanding the post to show the body.If you add the &#123;&#123;action&#125;&#125; helper to an HTML element, when a user clicks the element, the named event will be sent to the template&#x27;s corresponding component or controller.&quot;</span> &#125;;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;);</span><br></pre></td></tr></table></figure>
<p>重写<code>model</code>回调，直接返回一个对象数据。</p>
<h4 id="2，编写action模板"><a href="#2，编写action模板" class="headerlink" title="2，编写action模板"></a>2，编写action模板</h4><figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">&lt;!--  //  app/templates/myaction.hbs  --&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="comment">&lt;!-- showDetailInfo这个事件的名字必须要跟controller里的方法名字一致 --&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">h2</span> &#123;&#123;<span class="attr">action</span> &#x27; <span class="attr">showDetailInfo</span> &#x27;&#125;&#125; <span class="attr">style</span>=<span class="string">&quot;cursor: pointer;&quot;</span>&gt;</span>&#123;&#123;model.title&#125;&#125;<span class="tag">&lt;/<span class="name">h2</span>&gt;</span></span><br><span class="line">&#123;&#123;#if isShowingBody&#125;&#125;</span><br><span class="line"><span class="tag">&lt;<span class="name">p</span>&gt;</span></span><br><span class="line">&#123;&#123;model.body&#125;&#125;</span><br><span class="line"><span class="tag">&lt;/<span class="name">p</span>&gt;</span></span><br><span class="line">&#123;&#123;/if&#125;&#125;</span><br></pre></td></tr></table></figure>
<p>默认下只显示文章的标题，当用户点击标题的时候触发事件<code>toggleBody</code>显示文章的详细信息。</p>
<h4 id="3，编写action的controller实现模板所需要的逻辑"><a href="#3，编写action的controller实现模板所需要的逻辑" class="headerlink" title="3，编写action的controller实现模板所需要的逻辑"></a>3，编写action的controller实现模板所需要的逻辑</h4><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// app/controllers/myaction.js</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> Ember <span class="keyword">from</span> <span class="string">&#x27;ember&#x27;</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> Ember.Controller.extend(&#123;</span><br><span class="line"></span><br><span class="line">    <span class="comment">//  控制页面文章详细内容是否显示</span></span><br><span class="line">    isShowingBody: <span class="literal">false</span>,</span><br><span class="line">    actions: &#123;</span><br><span class="line">        showDetailInfo: <span class="function"><span class="keyword">function</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">            <span class="comment">// toggleProperty方法直接把isShowingBody设置为相反值</span></span><br><span class="line">            <span class="comment">// toggleProperty方法详情：http://devdocs.io/ember/classes/ember.observable#method_toggleProperty</span></span><br><span class="line">            <span class="built_in">this</span>.toggleProperty(<span class="string">&#x27;isShowingBody&#x27;</span>);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;);</span><br></pre></td></tr></table></figure>
<p>对于<code>controller</code>的处理逻辑你还可以直接编写触发的判断。</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line">actions: &#123;</span><br><span class="line">        showDetailInfo: <span class="function"><span class="keyword">function</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">            <span class="comment">// toggleProperty方法直接把isShowingBody设置为相反值</span></span><br><span class="line">            <span class="comment">// toggleProperty方法详情：http://devdocs.io/ember/classes/ember.observable#method_toggleProperty</span></span><br><span class="line">            <span class="comment">// this.toggleProperty(&#x27;isShowingBody&#x27;);</span></span><br><span class="line">            </span><br><span class="line">            <span class="comment">// 变量作用域问题</span></span><br><span class="line">            <span class="keyword">var</span> isShowingBody = <span class="built_in">this</span>.get(<span class="string">&#x27;isShowingBody&#x27;</span>);</span><br><span class="line">            <span class="keyword">if</span> (isShowingBody) &#123;</span><br><span class="line">                <span class="built_in">this</span>.set(<span class="string">&#x27;isShowingBody&#x27;</span>, <span class="literal">false</span>);    </span><br><span class="line">            &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">                <span class="built_in">this</span>.set(<span class="string">&#x27;isShowingBody&#x27;</span>, <span class="literal">true</span>);</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br></pre></td></tr></table></figure>
<p>如果你不使用<code>toggleProperty</code>方法改变<code>isShowingBody</code>的值，你也可用直接编写代码修改它的值。<br>最后执行URL：<a target="_blank" rel="noopener" href="http://localhost:4200/myaction">http://localhost:4200/myaction</a>，默认情况下页面上是不显示文章的详细信息的，当你点击标题则会触发事件，显示详细信息，下面2个图片分别展示的是默认情况和点击标题之后。当我们再次点击标题，详细内容又变为隐藏。</p>
<p><img src="/content/images/2016/03/35.png" alt="图片1"></p>
<p><img src="/content/images/2016/03/36-1.png" alt="图片2"></p>
<p>通过上述的小例子可以看到<code>action</code>助手使用起来也是非常简单的。主要注意下模板上的<code>action</code>所指定的事件名称要与<code>controller</code>里的方法名字一致。</p>
<h3 id="2，action参数"><a href="#2，action参数" class="headerlink" title="2，action参数"></a>2，action参数</h3><p>就像调用<code>javascript</code>的方法一样，你也可以为<code>action</code>助手增加必要的参数。只要在<code>action</code>名字后面接上你的参数即可。</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">p</span>&gt;</span></span><br><span class="line"><span class="comment">&lt;!-- model直接作为参数传递到controller --&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">button</span> &#123;&#123;<span class="attr">action</span> &#x27;<span class="attr">hitMe</span>&#x27; <span class="attr">model</span>&#125;&#125;&gt;</span>点击我吧<span class="tag">&lt;/<span class="name">button</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">p</span>&gt;</span></span><br></pre></td></tr></table></figure>
<p>对应的在<code>controller</code>增加处理的方法<code>selected</code>。在此方法内打印获取到的参数值。</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// app/controllers/myaction.js</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> Ember <span class="keyword">from</span> <span class="string">&#x27;ember&#x27;</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> Ember.Controller.extend(&#123;</span><br><span class="line"></span><br><span class="line">    <span class="comment">//  控制页面文章详细内容是否显示</span></span><br><span class="line">    isShowingBody: <span class="literal">false</span>,</span><br><span class="line">    actions: &#123;</span><br><span class="line">        showDetailInfo: <span class="function"><span class="keyword">function</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">            <span class="comment">//  ⋯⋯同上面的例子</span></span><br><span class="line">        &#125;,</span><br><span class="line">        hitMe: <span class="function"><span class="keyword">function</span>(<span class="params">model</span>) </span>&#123;   <span class="comment">//  参数的名字可以任意</span></span><br><span class="line">            <span class="built_in">console</span>.log(<span class="string">&#x27;The title is &#x27;</span> + model.title);</span><br><span class="line">            <span class="built_in">console</span>.log(<span class="string">&#x27;The body is &#x27;</span> + model.body);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;);</span><br></pre></td></tr></table></figure>

<p>Ember规定我们编写的动作处理的方法都是放在<code>actions</code>这个哈希内。哈希的键就是方法名。在<code>controller</code>方法上的参数名不要求与模板上传递的名字一致，你可以任意定义。比如方法<code>hitMe</code>的参数<code>model</code>你也可以使用<code>m</code>作为<code>hitMe</code>方法的参数。</p>
<p>当用户点击按钮“点击我吧”就会触发方法<code>hitMe</code>，然后执行<code>controller</code>的同名方法，最后你可以在浏览器的<code>console</code>下看到如下的打印信息。</p>
<p><img src="/content/images/2016/03/37.png" alt="run result"></p>
<p>看到这些打印结果很好的说明了获取的参数是正确的。</p>
<h3 id="3，指定action触发的事件类型"><a href="#3，指定action触发的事件类型" class="headerlink" title="3，指定action触发的事件类型"></a>3，指定action触发的事件类型</h3><p>默认情况下<code>action</code>触发的是<code>click</code>事件，你可以指定其他事件，比如键盘按下事件<code>keypress</code>。事件的名字与<code>javascript</code>提供的名字是相似的，唯一不同的是Ember所识别是事件名字如果是由不同单词组成的需要用中划线分隔，比如<code>keypress</code>事件在Ember模板中你需要写成<code>key-press</code>。<br>注意：你指定事件的时候要把事件的名字作为<code>on</code>的属性。比如<code>on=&#39;key-press&#39;</code>。</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">a</span> <span class="attr">href</span>=<span class="string">&quot;#/myaction&quot;</span> &#123;&#123;<span class="attr">action</span> &#x27;<span class="attr">triggerMe</span>&#x27; <span class="attr">on</span>=<span class="string">&quot;mouse-over&quot;</span>&#125;&#125;&gt;</span>鼠标移到我身上触发<span class="tag">&lt;/<span class="name">a</span>&gt;</span></span><br></pre></td></tr></table></figure>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">triggerMe: <span class="function"><span class="keyword">function</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">    <span class="built_in">console</span>.log(<span class="string">&#x27;触发mouseover事件。。。。&#x27;</span>);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="4，指定action触发事件的辅助按键"><a href="#4，指定action触发事件的辅助按键" class="headerlink" title="4，指定action触发事件的辅助按键"></a>4，指定<code>action</code>触发事件的辅助按键</h3><p>甚至你还可以指定按下键盘某个键后点击才触发<code>action</code>所指定的事件，比如按下键盘的<code>Alt</code>再点击才会触发事件。使用<code>allowedkeys</code>属性指定按下的是那个键。</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">br</span>&gt;</span><span class="tag">&lt;<span class="name">br</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">button</span> &#123;&#123;<span class="attr">action</span> &#x27;<span class="attr">pressALTKeyTiggerMe</span>&#x27; <span class="attr">allowedkeys</span>=<span class="string">&#x27;alt&#x27;</span>&#125;&#125;&gt;</span>按下Alt点击触发我<span class="tag">&lt;/<span class="name">button</span>&gt;</span></span><br></pre></td></tr></table></figure>

<h3 id="5，禁止标签默认行为"><a href="#5，禁止标签默认行为" class="headerlink" title="5，禁止标签默认行为"></a>5，禁止标签默认行为</h3><p>在<code>action</code>助手内使用属性<code>preventDefault=false</code>可以禁止标签的默认行为，比如下面的a标签，如果<code>action</code>助手内没有定义这个属性那么你点击链接时只会执行执行的<code>action</code>动作，<code>a</code>标签默认的行为不会被触发。</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">a</span> <span class="attr">href</span>=<span class="string">&quot;http://www.baidu.com&quot;</span> &#123;&#123;<span class="attr">action</span> &quot;<span class="attr">showDetailInfo</span>&quot; <span class="attr">preventDefault</span>=<span class="string">false&#125;&#125;</span>&gt;</span></span><br><span class="line">点我跳转</span><br><span class="line"><span class="tag">&lt;/<span class="name">a</span>&gt;</span></span><br></pre></td></tr></table></figure>

<h3 id="6，可以把触发的事件作为参数传递到controller"><a href="#6，可以把触发的事件作为参数传递到controller" class="headerlink" title="6，可以把触发的事件作为参数传递到controller"></a>6，可以把触发的事件作为参数传递到<code>controller</code></h3><p><code>handlebars</code>的<code>action</code>助手真的是非常强大，你甚至可以把触发的事件作为<code>action</code>的参数直接传递到<code>controller</code>。不过你需要把<code>action</code>助手放在<code>javascript</code>的事件里。比如下面的代码当失去焦点时触发，并且通过<code>action</code>指定的<code>dandDidChange</code>把触发的事件<code>blur</code>传递到<code>controller</code>。</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">label</span>&gt;</span>失去焦点时候触发<span class="tag">&lt;/<span class="name">label</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">input</span> <span class="attr">type</span>=<span class="string">&quot;text&quot;</span> <span class="attr">value</span>=<span class="string">&#123;&#123;textValue&#125;&#125;</span> <span class="attr">onblur</span>=<span class="string">&#123;&#123;action</span> &#x27;<span class="attr">bandDidChange</span>&#x27;&#125;&#125; /&gt;</span></span><br></pre></td></tr></table></figure>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// app/controllers/myaction.js</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> Ember <span class="keyword">from</span> <span class="string">&#x27;ember&#x27;</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> Ember.Controller.extend(&#123;</span><br><span class="line"></span><br><span class="line">    actions: &#123;</span><br><span class="line">        bandDidChange: <span class="function"><span class="keyword">function</span>(<span class="params">event</span>) </span>&#123;</span><br><span class="line">            <span class="built_in">console</span>.log(<span class="string">&#x27;event = &#x27;</span> + event);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    </span><br><span class="line">&#125;);</span><br></pre></td></tr></table></figure>

<p><img src="/content/images/2016/03/38.png" alt="result"></p>
<p>从控制台输出结果看出来<code>event</code>的值是一个对象并且是一个<code>focus</code>事件。<br>但是如果你在<code>action</code>助手内增加一个属性<code>value=&#39;target.value&#39;</code>(别写错只能是<code>target.value</code>)之后，传递到<code>controller</code>的则是输入框本身的内容。不再是事件对象本身。</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">input</span> <span class="attr">type</span>=<span class="string">&quot;text&quot;</span> <span class="attr">value</span>=<span class="string">&#123;&#123;textValue&#125;&#125;</span> <span class="attr">onblur</span>=<span class="string">&#123;&#123;action</span> &#x27;<span class="attr">bandDidChange</span>&#x27; <span class="attr">value</span>=<span class="string">&quot;target.value&quot;</span>&#125;&#125; /&gt;</span></span><br></pre></td></tr></table></figure>

<p><img src="/content/images/2016/03/39.png" alt="result"></p>
<p>这个比较有意思，实现的功能与前面的参数传递类似的。</p>
<h3 id="7，action助手用在非点击元素上"><a href="#7，action助手用在非点击元素上" class="headerlink" title="7，action助手用在非点击元素上"></a>7，<code>action</code>助手用在非点击元素上</h3><pre><code>`action`助手可以用在任何的`DOM`元素上，不仅仅是用在能点击的元素上（比如`a`、`button`），但是用在其他非点击的元素上默认情况下是不可用的，也就是说点击也是无效的。比如用在`div`标签上，但是你点击`div`元素是没有反应的。如果你需要让`div`元素也能触发单击事件你需要给元素添加一个CSS类&#39;cursor:pointer;`。</code></pre>
<p>总的来说Ember的<code>action</code>助手与普通的javascript的事件是差不多的。用法基本上与javascript的事件相似。</p>
<br>
博文完整代码放在[Github](https://github.com/ubuntuvim/my_emberjs_code)（博文经过多次修改，博文上的代码与github代码可能又出入，不过影响不大！），如果你觉得博文对你有点用，请在github项目上给我点个`star`吧。您的肯定对我来说是最大的动力！！

    </div>

      
    <div class="post-nav">
      <div class="post-nav-item post-nav-next">
        
          <span>〈 </span>
          <a href="/2020/01/18/ember-teach/templetes/handlebars%E6%98%BE%E7%A4%BA%E5%AF%B9%E8%B1%A1%E7%9A%84%E9%94%AE/" rel="next" title="handlebars显示对象的键">
          handlebars显示对象的键
          </a>
        
      </div>
  
      <div class="post-nav-item post-nav-prev">
          
          <a href="/2020/01/18/ember-teach/templetes/handlebars%E5%B1%9E%E6%80%A7%E7%BB%91%E5%AE%9A/" rel="prev" title="handlebars属性绑定">
            handlebars属性绑定
          </a>
          <span>〉</span>
        
      </div>
    </div>
  
  </article>
  <div class="toc-container">
    
  <div id="toc" class="toc-article">
    <strong class="toc-title">目录</strong>
    <ol class="toc"><li class="toc-item toc-level-3"><a class="toc-link" href="#1%EF%BC%8Caction%E4%BD%BF%E7%94%A8%E5%AE%9E%E4%BE%8B"><span class="toc-text">1，action使用实例</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#1%EF%BC%8C%E5%9C%A8route%E5%B1%82%E5%A2%9E%E5%8A%A0%E6%B5%8B%E8%AF%95%E6%95%B0%E6%8D%AE"><span class="toc-text">1，在route层增加测试数据</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#2%EF%BC%8C%E7%BC%96%E5%86%99action%E6%A8%A1%E6%9D%BF"><span class="toc-text">2，编写action模板</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#3%EF%BC%8C%E7%BC%96%E5%86%99action%E7%9A%84controller%E5%AE%9E%E7%8E%B0%E6%A8%A1%E6%9D%BF%E6%89%80%E9%9C%80%E8%A6%81%E7%9A%84%E9%80%BB%E8%BE%91"><span class="toc-text">3，编写action的controller实现模板所需要的逻辑</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#2%EF%BC%8Caction%E5%8F%82%E6%95%B0"><span class="toc-text">2，action参数</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#3%EF%BC%8C%E6%8C%87%E5%AE%9Aaction%E8%A7%A6%E5%8F%91%E7%9A%84%E4%BA%8B%E4%BB%B6%E7%B1%BB%E5%9E%8B"><span class="toc-text">3，指定action触发的事件类型</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#4%EF%BC%8C%E6%8C%87%E5%AE%9Aaction%E8%A7%A6%E5%8F%91%E4%BA%8B%E4%BB%B6%E7%9A%84%E8%BE%85%E5%8A%A9%E6%8C%89%E9%94%AE"><span class="toc-text">4，指定action触发事件的辅助按键</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#5%EF%BC%8C%E7%A6%81%E6%AD%A2%E6%A0%87%E7%AD%BE%E9%BB%98%E8%AE%A4%E8%A1%8C%E4%B8%BA"><span class="toc-text">5，禁止标签默认行为</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#6%EF%BC%8C%E5%8F%AF%E4%BB%A5%E6%8A%8A%E8%A7%A6%E5%8F%91%E7%9A%84%E4%BA%8B%E4%BB%B6%E4%BD%9C%E4%B8%BA%E5%8F%82%E6%95%B0%E4%BC%A0%E9%80%92%E5%88%B0controller"><span class="toc-text">6，可以把触发的事件作为参数传递到controller</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#7%EF%BC%8Caction%E5%8A%A9%E6%89%8B%E7%94%A8%E5%9C%A8%E9%9D%9E%E7%82%B9%E5%87%BB%E5%85%83%E7%B4%A0%E4%B8%8A"><span class="toc-text">7，action助手用在非点击元素上</span></a></li></ol>
  </div>


  </div>
</div>


<div class="copyright">
    <span>本作品采用</span>
    <a target="_blank" rel="noopener" href="https://creativecommons.org/licenses/by/4.0/">知识共享署名 4.0 国际许可协议</a>
    <span>进行许可。 转载时请注明原文链接。</span>
</div>
<div class="share">

</div>
<div class="post-container">
    <article class="post">
      <div id="container"></div>
    </article>
</div>

<link rel="stylesheet" href="https://imsun.github.io/gitment/style/default.css">
<script src="https://imsun.github.io/gitment/dist/gitment.browser.js"></script>
<script>
var gitment = new Gitment({
  id: 'action helper', // 可选。默认为 location.href
  owner: 'ubuntuvim',
  repo: 'ubuntuvim.github.io',
  oauth: {
    client_id: '48b3df0bf4ba1743e0a3',
    client_secret: 'c6c2cec09acb5eecc9d325d56cef187264aa1e16',
  },
})
gitment.render('container')
</script>



    </div>

    

  </div>
  <footer class="footer text-center">
    <div id="bottom-inner">
        <a class="bottom-item" href="http://xcoding.tech/">首页</a> |
        <a class="bottom-item" href="http://xcoding.tech/" target="_blank">主站</a> |
        <a class="bottom-item" href="https://github.com/ubuntuvim" target="_blank">GitHub</a> |
        <a class="bottom-item" href="https://hexo.io" target="_blank">Powered by hexo</a> |
        <a class="bottom-item" href="https://github.com/KevinOfNeu/hexo-theme-xoxo" target="_blank">Theme xoxo</a>
    </div>
</footer>


  <script src='https://unpkg.com/mermaid@7.1.2/dist/mermaid.min.js'></script>
  <script>
    if (window.mermaid) {
      mermaid.initialize({theme: 'forest'});
    }
  </script>


  
  <!-- scripts list from theme config.yml -->
  
    <script src="/js/zepto.min.js"></script>
  


<script>
  (function(window, document, undefined) {

    var timer = null;

    function returnTop() {
      cancelAnimationFrame(timer);
      timer = requestAnimationFrame(function fn() {
        var oTop = document.body.scrollTop || document.documentElement.scrollTop;
        if (oTop > 0) {
          document.body.scrollTop = document.documentElement.scrollTop = oTop - 50;
          timer = requestAnimationFrame(fn);
        } else {
          cancelAnimationFrame(timer);
        }
      });
    }

    var hearts = [];
    window.requestAnimationFrame = (function() {
      return window.requestAnimationFrame ||
        window.webkitRequestAnimationFrame ||
        window.mozRequestAnimationFrame ||
        window.oRequestAnimationFrame ||
        window.msRequestAnimationFrame ||
        function(callback) {
          setTimeout(callback, 1000 / 60);
        }
    })();
    init();

    function init() {
      css(".heart{z-index:9999;width: 10px;height: 10px;position: fixed;background: #f00;transform: rotate(45deg);-webkit-transform: rotate(45deg);-moz-transform: rotate(45deg);}.heart:after,.heart:before{content: '';width: inherit;height: inherit;background: inherit;border-radius: 50%;-webkit-border-radius: 50%;-moz-border-radius: 50%;position: absolute;}.heart:after{top: -5px;}.heart:before{left: -5px;}");
      attachEvent();
      gameloop();
      addMenuEvent();
    }

    function gameloop() {
      for (var i = 0; i < hearts.length; i++) {
        if (hearts[i].alpha <= 0) {
          document.body.removeChild(hearts[i].el);
          hearts.splice(i, 1);
          continue;
        }
        hearts[i].y--;
        hearts[i].scale += 0.004;
        hearts[i].alpha -= 0.013;
        hearts[i].el.style.cssText = "left:" + hearts[i].x + "px;top:" + hearts[i].y + "px;opacity:" + hearts[i].alpha + ";transform:scale(" + hearts[i].scale + "," + hearts[i].scale + ") rotate(45deg);background:" + hearts[i].color;
      }
      requestAnimationFrame(gameloop);
    }

    /**
     * 给logo设置点击事件
     * 
     * - 回到顶部
     * - 出现爱心
     */
    function attachEvent() {
      var old = typeof window.onclick === "function" && window.onclick;
      var logo = document.getElementById("logo");
      if (logo) {
        logo.onclick = function(event) {
          returnTop();
          old && old();
          createHeart(event);
        }
      }
      
    }

    function createHeart(event) {
      var d = document.createElement("div");
      d.className = "heart";
      hearts.push({
        el: d,
        x: event.clientX - 5,
        y: event.clientY - 5,
        scale: 1,
        alpha: 1,
        color: randomColor()
      });
      document.body.appendChild(d);
    }

    function css(css) {
      var style = document.createElement("style");
      style.type = "text/css";
      try {
        style.appendChild(document.createTextNode(css));
      } catch (ex) {
        style.styleSheet.cssText = css;
      }
      document.getElementsByTagName('head')[0].appendChild(style);
    }

    function randomColor() {
      // return "rgb(" + (~~(Math.random() * 255)) + "," + (~~(Math.random() * 255)) + "," + (~~(Math.random() * 255)) + ")";
      return "#F44336";
    }

    function addMenuEvent() {
      var menu = document.getElementById('menu-main-post');
      if (menu) {
        var toc = document.getElementById('toc');
        if (toc) {
          menu.onclick = function() {
            if (toc) {
              if (toc.style.display == 'block') {
                toc.style.display = 'none';
              } else {
                toc.style.display = 'block';
              }
            }
          };
        } else {
          menu.style.display = 'none';
        }
      }
    }

  })(window, document);
</script>

  



</body>
</html>
